package com.xm.xmapp.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;

import com.xm.xmapp.R;

public class MyProgressBar extends LinearLayout {

	private TypedArray mArray;
	private static final int NO_VALUE = -1;
	private Animation animation;

	public MyProgressBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		inflate(context, R.layout.view_progressbar, this);
		mArray = context.obtainStyledAttributes(attrs,
				R.styleable.MyProgressBar);
		drawProgressBar();
		show();
		mArray.recycle();
	}

	public void show() {

		rotate();

	}

	private void rotate() {
		if (animation == null) {
			animation = AnimationUtils.loadAnimation(getContext(),
					R.anim.loading_dialog_round);
			animation.setRepeatCount(Animation.INFINITE);
			animation.setRepeatMode(Animation.RESTART);
		}
		this.startAnimation(animation);
	}

	private void drawProgressBar() {
		int drawable = mArray.getResourceId(R.styleable.MyProgressBar_drawable,
				NO_VALUE);
		if (drawable != NO_VALUE) {
			this.setBackgroundResource(drawable);
		} else {
			this.setBackgroundResource(R.mipmap.loading_black);
		}
	}

	public void cancel() {

		MyProgressBar.this.clearAnimation();
		if (animation != null) {
			animation.cancel();
		}

	}
}
